var proxy = require('http-proxy-middleware')
var target = require('../env.config').host

var myProxy = proxy('/api', {
  target,
  changeOrigin: true,
  logLevel: 'debug',
  pathRewrite: {
    '^/api': '/'
  },
  onProxyReq(proxyReq, req, res) {
    // TODO
  },
  onProxyRes(proxyRes, req, res) {
    // TODO
    // sails.log.debug(res)
  }
})

/**
 * 修改 header，使其包含用户信息
 */
function addUserInfo(req) {
  // TODO
  req.headers.staffid = 5972
  req.headers.companyid = 2
}

/**
 * 向 java 后台请求的代理
 */
module.exports = function (req, res, next) {
  addUserInfo(req)

  myProxy(req, res, next)
}
